#include "core/main.h"

void GameTestBasic::Init()
{
    _collection.AddSprite(Bitmap::LoadFile("../assets/testBasic/character.png"), "character");
    _collection.AddSprite(Bitmap::LoadFile("../assets/testBasic/tile0.png"), "tile0");
    _collection.AddSprite(Bitmap::LoadFile("../assets/testBasic/tile1.png"), "tile1");
    _collection.AddSprite(Bitmap::LoadFile("../assets/testBasic/tile2.png"), "tile2");
    
    _observedIntNames.push_back("characterX");
    _observedIntNames.push_back("characterY");
    
    _tiles.Allocate(Constants::NESGridHeight, Constants::NESGridWidth);
}

void GameTestBasic::NewGame()
{
    for(UINT y = 0; y < _tiles.Rows(); y++)
    {
        for(UINT x = 0; x < _tiles.Cols(); x++)
        {
            _tiles(y, x) = rand() % 3 + 1;
        }
    }

    _charaterPos = Vec2i(20, 20);
}

ControllerState GameTestBasic::SimpleAI()
{
    ControllerState result;
    return result;
}

void GameTestBasic::Step(ControllerState &controller)
{
    if(controller.up()   ) _charaterPos.y -= 4;
    if(controller.down() ) _charaterPos.y += 4;
    if(controller.left() ) _charaterPos.x -= 4;
    if(controller.right()) _charaterPos.x += 4;

    _charaterPos.x = Math::Bound(_charaterPos.x, 0, int(Constants::NESWidth ) - 16);
    _charaterPos.y = Math::Bound(_charaterPos.y, 0, int(Constants::NESHeight) - 16);

    MakeFrameSprites();
}

void GameTestBasic::MakeFrameSprites()
{
    _activeFrame.sprites.clear();
    for(UINT y = 0; y < _tiles.Rows(); y++)
    {
        for(UINT x = 0; x < _tiles.Cols(); x++)
        {
            _activeFrame.sprites.push_back(SpriteLocation(Vec2i(x * 8, y * 8), _tiles(y, x), 0));
        }
    }

    _activeFrame.sprites.push_back(SpriteLocation(_charaterPos, 0, 1));
}
